//
//  MySidebar.m
//  何五路
//
//  Created by _ADY on 15/7/6.
//  Copyright (c) 2015年 _ADY. All rights reserved.
//

#import "MySidebar.h"

@implementation MySidebar
@synthesize sidebarArray,_image,_imageSS,selectedIndex,delegate;
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
       
    }
    return self;
}
-(void)setTitle:(NSArray*)mTitle setImage:(NSArray*)image setSeclet:(NSArray*)images setBOOL:(BOOL)setB
{
    sidebarArray = mTitle;
    _image = image;
    _imageSS = images;
    if (setB)
     [self gotoFview];
}
-(void)gotoFview
{
    UIView *bView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    bView.backgroundColor = [UIColor whiteColor];
    [self addSubview:bView];
    
    for (int i = 0; i < sidebarArray.count; i ++)
    {
        NSString *title = [sidebarArray objectAtIndex:i];
        NSString *image = [_image objectAtIndex:i];
        UIImage *imageD = [UIImage imageNamed:[NSString stringWithFormat:@"%@",image]];
        
        UIButton *inButton = [UIButton buttonWithType:UIButtonTypeCustom];
        CGRect frame = CGRectMake(0,0, self.frame.size.width/4, self.frame.size.height);
        [inButton setImage:imageD forState:UIControlStateNormal];
        [inButton setFrame:frame];
        [inButton setTitleEdgeInsets:UIEdgeInsetsMake(0, imageD.size.width, 0, 0)];
        [inButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        
        [inButton setTitle:title forState:UIControlStateNormal];
        [inButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        inButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize+1];
        inButton.center = CGPointMake(self.frame.size.width*i/sidebarArray.count+self.frame.size.width/(sidebarArray.count*2), self.frame.size.height/2);
        inButton.tag = i+1;
        [inButton addTarget:self action:@selector(aInBction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:inButton];
        if (i != 0 ) {
            UIView *b2View = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width*i/sidebarArray.count, self.frame.size.height/4, .5, self.frame.size.height/2)];
            b2View.backgroundColor = [UIColor colorWithWhite:.8 alpha:1];
            [self addSubview:b2View];
        }
        
    }
    UIView *b1View = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height-1, self.frame.size.width, 1)];
    b1View.backgroundColor = [UIColor grayColor];
    [self addSubview:b1View];
    
    selectView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height-2, self.frame.size.width/sidebarArray.count, 2)];
    selectView.backgroundColor = [UIColor redColor];
    [self addSubview:selectView];
    
}
-(void)aInBction:(id)sender
{
    int type = ((UIButton*)sender).tag-1;
    if (type != selectedIndex) {
        selectedIndex = type;
        [self gotoSet:YES];
    }
}

- (void)sSelectedSegmentIndex:(NSInteger)selectedSegmentIndex
{
    if (selectedIndex != selectedSegmentIndex)
    {
        selectedIndex = selectedSegmentIndex;
        [self gotoSet:NO];
    }
}

- (void)setSelectedSegmentIndex:(NSInteger)selectedSegmentIndex
{
    if (selectedIndex != selectedSegmentIndex)
    {
        selectedIndex = selectedSegmentIndex;
        [self gotoSet:YES];
    }
}

-(void)gotoSet:(BOOL)aYes
{
    for (int i = 0; i < sidebarArray.count; i ++)
    {
        UIButton *btButton = (UIButton*)[self viewWithTag:i+1];
        if (i == selectedIndex)
        {
            NSString *image = [_imageSS objectAtIndex:i];
            UIImage *imageD = [UIImage imageNamed:[NSString stringWithFormat:@"%@",image]];
            
            [btButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            [btButton setImage:imageD forState:UIControlStateNormal];
        }
        else
        {
            NSString *image = [_image objectAtIndex:i];
            UIImage *imageD = [UIImage imageNamed:[NSString stringWithFormat:@"%@",image]];
            
            [btButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [btButton setImage:imageD forState:UIControlStateNormal];
        }
    }
    [UIView animateWithDuration:.5 animations:^{
        selectView.frame = CGRectMake((selectedIndex*self.frame.size.width)/sidebarArray.count, self.frame.size.height-2, self.frame.size.width/sidebarArray.count, 2);
    }];
    if (aYes)
        [self.delegate MySidebar:selectedIndex];
    
}

- (void)removeAllSegments
{
    selectedIndex = -1;
    [self removeFromSuperview];
    
}

@end
